package org.ssp.test;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.MailSSLSocketFactory;

/*
 * 正文内嵌图片的邮件
 * 
 */
public class EmailTest2 {

	public static void main(String[] args) {
		Properties prop=new Properties();
		prop.setProperty("mail.host", "smtp.qq.com");
		prop.setProperty("mail.transport.protocol", "smtp");
		prop.setProperty("mail.smtp.auth", "true");
		MailSSLSocketFactory sf=null;
		//ssl加密
		try {
			sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);

		} catch (GeneralSecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.ssl.socketFactory", sf);
		//使用javamail发送邮件的五个步骤
		//1，创建session对象
		Session session =Session.getInstance(prop);
		//开启debug描模式
		session.setDebug(true);
		//2.通过session得到transport
		Transport  ts=null;
		try {
			ts=session.getTransport();
			//3，创建邮件
			Message message=createImageMail(session);
			//4，连接服务器，用户名和密码验证
			ts.connect("smtp.qq.com","1255292979","slhphqvgsujkjbgi");//密码为授权码
			//5.发送邮件
			ts.sendMessage(message, message.getAllRecipients());
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				ts.close();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
	
	public static MimeMessage createImageMail(Session session){
		//创建邮件
		MimeMessage message=new MimeMessage(session);
		try {
			//发送人
			message.setFrom(new InternetAddress("1255292979@qq.com"));
			//接收人
			message.setRecipient(Message.RecipientType.TO, new InternetAddress("1255292979@qq.com"));
			//标题
			message.setSubject("带图片的邮件");
			
			//准备邮件数据
			MimeMultipart mm=new MimeMultipart();
			
			//准备邮件正文数据
			MimeBodyPart text=new MimeBodyPart();
			text.setContent("这个是一封邮件正文带图片<img src='cid:xxx.jpg'>的邮件","text/html;charset=utf-8");
			//准备图片数据
			MimeBodyPart image=new MimeBodyPart();
			String url=null;
			DataHandler dh=new DataHandler(new FileDataSource("src\\login-bg.jpg"));
			image.setDataHandler(dh);
//			image.setHeader("Content-ID","<image>");
			image.setContentID("xxx.jpg");

			//描述数据关系
			mm.addBodyPart(text);
			mm.addBodyPart(image);
			mm.setSubType("related");
			message.setContent(mm);
			message.saveChanges();
			//将创建好的邮件写入到e盘以文件的形式保存
//			message.writeTo(new FileOutputStream(""));
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
	}
}
